//二分查找
public class BinarySearch {
    private BinarySearch() {
    }

    //非递归实现二分查找
    public static <E extends Comparable<E>> int search(E[] data, E target) {
        int l = 0, r = data.length - 1;
        while (l <= r) {
            int mid = l + (r - l) / 2;
            if (data[mid].compareTo(target) == 0) {
                return mid;
            } else if (data[mid].compareTo(target) < 0) {
                //缩小左边界
                l = mid + 1;
            } else {
                //缩小右边界
                r = mid - 1;
            }
        }
        return -1;
    }
}
